import { createRouter, createWebHistory } from 'vue-router'
import routes from './routes'

const router = createRouter({
  // history: createWebHistory(import.meta.env.BASE_URL),
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const existingRoute = router.getRoutes().find(route => route.path === to.path);
  // 处理中文path
  if (!existingRoute) {
    const decodedPath = decodeURIComponent(to.path);
    const decodedRoute = router.getRoutes().find(route => route.path === decodedPath);
    if (decodedRoute) {
      // 如果解码后匹配到了路由，重定向到解码后的路径对应的路由
      next({ ...to, path: decodedPath });
      return
    }
  }
  next();
});

export default router
